import { createSlice } from "@reduxjs/toolkit";

const cart = createSlice({
  name: "cart",

  initialState: {
    cartlist: [
      { id: 1, name: "第一班", start: "00:00", end: "02:59", sx: 20, zx: 20 },
      { id: 2, name: "第二班", start: "03:00", end: "05:59", sx: 20, zx: 20 },
      { id: 3, name: "第三班", start: "06:00", end: "13:59", sx: 20, zx: 20 },
      { id: 4, name: "第四班", start: "14:00", end: "19:59", sx: 20, zx: 20 },
      { id: 5, name: "第五班", start: "20:00", end: "23:59", sx: 20, zx: 20 },
    ],
  },

  reducers: {
    addItem: (state, action) => {
      state.cartlist.push(action.payload);
    },
    removeItem: (state, action) => {
      state.cartlist = state.cartlist.filter(
        (item) => item.id !== action.payload
      );
    },
    updateItem: (state, action) => {
      const { id, changes } = action.payload;
      const item = state.cartlist.find((item) => item.id === id);
      if (item) {
        Object.assign(item, changes);
      }
    },
  },
});

export const { addItem, removeItem, updateItem } = cart.actions;

export default cart;
